import qdarkstyle
from PyQt5.QtWidgets import QMessageBox
from qtpy import QtCore

from src.admin.AllFine_UI import Ui_Form
from PyQt5 import QtWidgets
from src.admin.InsertFine_win import InsertFine_Win
import mysql.connector


class AllFine_Win(QtWidgets.QMainWindow,Ui_Form):
    def __init__(self, mydb,parent=None):
        super(AllFine_Win, self).__init__(parent=parent)
        #ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('')
        #self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
        #self.setWindowIcon(icon)
        self.setupUi(self)
        self.LoginSignal=False
        self.mydb = mydb

        self.Insert_form=InsertFine_Win(self.mydb)
        self.Insert_form.setWindowTitle("新增罚款")


        self._translate = QtCore.QCoreApplication.translate
        self.tableWidget.setEditTriggers(self.tableWidget.NoEditTriggers)
        self.Insert_form.pushButton.clicked.connect(self.myFineInsert)

    def Table_Data(self, i, j, data):

        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(i, j, item)
        item = self.tableWidget.item(i, j)
        item.setText(self._translate("Form", str(data)))

    def myTable_init(self):
        cur = self.mydb.cursor()
        cur.execute('select * from 罚款信息表')  # 将数据从数据库中拿出来
        total = cur.fetchall()
        col_result = cur.description
        self.row = cur.rowcount  # 取得记录个数，用于设置表格的行数
        self.vol = len(total[0])  # 取得字段数，用于设置表格的列数
        col_result = list(col_result)
        a = 0
        self.tableWidget.setColumnCount(self.vol)
        self.tableWidget.setRowCount(self.row)
        for i in col_result:  # 设置表头信息，将mysql数据表中的表头信息拿出来，放进TableWidget中
            item = QtWidgets.QTableWidgetItem()
            self.tableWidget.setHorizontalHeaderItem(a, item)
            item = self.tableWidget.horizontalHeaderItem(a)
            item.setText(self._translate("Form", i[0]))
            a = a + 1
        total = list(total)  # 将数据格式改为列表形式，其是将数据库中取出的数据整体改为列表形式
        for i in range(len(total)):  # 将相关的数据
            total[i] = list(total[i])  # 将获取的数据转为列表形式
        for i in range(self.row):
            for j in range(self.vol):
                self.Table_Data(i, j, total[i][j])

    def myInsert(self):
        self.Insert_form.show()

    def myFineInsert(self):
        try:
            data = (int(self.Insert_form.lineEdit.text()), int(self.Insert_form.lineEdit_2.text()),
                    int(self.Insert_form.lineEdit_3.text()), float(self.Insert_form.label_7.text()),
                    self.Insert_form.dateEdit.text())
        except Exception as a:
            QMessageBox.critical(self, '错误', '请输入数据')
        else:
            if float(self.Insert_form.label_7.text()) != 0:
                try:
                    mycusor = self.mydb.cursor()
                    mycusor.execute(
                        "insert into 罚款信息表(会员号, 玩具编号, 罚款种类号,罚款扣点, 罚款日期) value('%d','%d','%d','%f','%s')" % (data))
                    self.mydb.commit()
                except Exception as a:
                    QMessageBox.critical(self, '错误', '插入失败')
                    print(a)
                else:
                    self.myTable_init()
            else:
                QMessageBox.critical(self, '错误', '请先计算')
    def getMydb(self,mydb):
        self.mydb=mydb
        self.Insert_form.getmydb(mydb)